*-------------------------------------------------------------------------------*
* DIRECTORIES
*-------------------------------------------------------------------------------*
local l_direc_data `1'
local l_direc_code `2' 
local l_direc_figtab `3' 

local direc_data_raw  `l_direc_data'/raw
local direc_data_clean `l_direc_data'/clean



*---------------------------------------------------------------------------------*
* DATA IN AND SAMPLE AND CLEAN
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/mjt_regready_agg050.dta", clear

keep if !regexm(journal_name, "(Rest of PubMed)")
keep if year >= 1985

foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all {
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}

gen X = .
gen Y = .


*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL A -- linear
*---------------------------------------------------------------------------------*
preserve

	replace X = eshr2_frcnt_all
	replace Y = jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
		
	* JM	
	qui {
		reghdfe Y X , a(fe_jm) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_jm)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jm) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
		
	* MT	
	qui {
		reghdfe Y X , a(fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
	esttab using"`l_direc_figtab'/tab_main2.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_elastse estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		replace
	eststo clear	
restore



*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL B -- 0/1
*---------------------------------------------------------------------------------*
preserve

	replace X = A_eshr2_frcnt_all
	replace Y = A_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
	* JM	
	qui {
		reghdfe Y X , a(fe_jm) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_jm)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jm) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
	* MT	
	qui {
		reghdfe Y X , a(fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, eyex(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
	esttab using"`l_direc_figtab'/tab_main2.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_elastse estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		append
	eststo clear	
restore



*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL C -- LOG
*---------------------------------------------------------------------------------*
preserve

	replace X = lg_eshr2_frcnt_all
	replace Y = lg_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, dydx(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
	* JM	
	qui {
		reghdfe Y X , a(fe_jm) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_jm)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jm) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, dydx(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
	* MT	
	qui {
		reghdfe Y X , a(fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, dydx(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		margins, dydx(X) atmeans
		estadd scalar estflg_elast = r(table)[1,1]
		estadd scalar estflg_elastse = r(table)[2,1]
		
	esttab using"`l_direc_figtab'/tab_main2.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_elastse estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		append
	eststo clear	
restore	


*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL A -- linear
*---------------------------------------------------------------------------------*
preserve

	replace X = eshr2_frcnt_all
	replace Y = jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))

		
	* JM	
	qui {
		reghdfe Y X , a(fe_jm) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_jm)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jm) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))
		
	* MT	
	qui {
		reghdfe Y X , a(fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))

	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))
		
	esttab using"`l_direc_figtab'/tab_main.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		replace
	eststo clear	
restore



*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL B -- 0/1
*---------------------------------------------------------------------------------*
preserve

	replace X = A_eshr2_frcnt_all
	replace Y = A_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		
	* JM	
	qui {
		reghdfe Y X , a(fe_jm) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_jm)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jm) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		
	* MT	
	qui {
		reghdfe Y X , a(fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		
	esttab using"`l_direc_figtab'/tab_main.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		append
	eststo clear	
restore



*---------------------------------------------------------------------------------*
* MAIN TABLE: PANEL C -- LOG
*---------------------------------------------------------------------------------*
preserve

	replace X = lg_eshr2_frcnt_all
	replace Y = lg_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* NO FE
	eststo clear
	qui {
		reg Y X 
			local l_r2tot = e(r2)
		reg Y 
			local estflg_par2_X = (`l_r2tot'-e(r2))/(1-e(r2))
	}
	eststo: reg Y X , cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
		
	* JM	
	qui {
		reghdfe Y X , a(fe_jm) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_jm)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jm) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
		
	* MT	
	qui {
		reghdfe Y X , a(fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]
		
	esttab using"`l_direc_figtab'/tab_main.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		append
	eststo clear	
restore	






*---------------------------------------------------------------------------------*
* R2 of RHS
*---------------------------------------------------------------------------------*
preserve

	replace X = eshr2_frcnt_all
	replace Y = jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)

	qui {
		reghdfe X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe X , a(fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
restore
preserve

	replace X = A_eshr2_frcnt_all
	replace Y = A_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)

	qui {
		reghdfe X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe X , a(fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
restore
preserve

	replace X = lg_eshr2_frcnt_all
	replace Y = lg_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)

	qui {
		reghdfe X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe X , a(fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}

restore	

esttab using"`l_direc_figtab'/tab_R2RHS.tex", mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars( estflg_r2tot estflg_par2_jm estflg_par2_mt ) ///
	sfmt(%5.3fc %5.3fc %5.3fc ) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	replace







